import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { User } from '../../auth/entities/user.entity';
import { Work } from '../../works/entities/work.entity';

/**
 * 评论实体
 */
@Entity('comments')
export class Comment {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ comment: '作品ID' })
  @Index()
  workId: number;

  @Column({ comment: '评论者ID' })
  @Index()
  userId: number;

  @Column({ type: 'text', comment: '评论内容' })
  content: string;

  @Column({
    nullable: true,
    comment: '父评论ID（一级评论为null，二级评论有值）',
  })
  @Index()
  parentId: number | null;

  @Column({ nullable: true, comment: '回复的用户ID（二级评论时标记回复谁）' })
  replyToUserId: number | null;

  @Column({ default: 0, comment: '点赞数' })
  likesCount: number;

  @Column({ default: 0, comment: '回复数（仅一级评论有效）' })
  repliesCount: number;

  @Column({ default: false, comment: '是否被删除' })
  isDeleted: boolean;

  @ManyToOne(() => User, { eager: true })
  @JoinColumn({ name: 'userId' })
  user: User;

  @ManyToOne(() => Work, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'workId' })
  work: Work;

  @ManyToOne(() => Comment, { nullable: true, onDelete: 'CASCADE' })
  @JoinColumn({ name: 'parentId' })
  parent: Comment | null;

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'replyToUserId' })
  replyToUser: User | null;

  @CreateDateColumn({ type: 'timestamp', comment: '创建时间' })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamp', comment: '更新时间' })
  updatedAt: Date;
}
